/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.tom.social.watcher.agent;

import jade.core.AID;
import jade.core.Agent;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import pl.tom.social.common.agent.behaviour.CancelBehaviour;
import pl.tom.social.common.jade.TaskBarProxy;
import pl.tom.social.watcher.agent.behaviour.WatcherBehaviour;
import pl.tom.social.watcher.jade.LocalJade;

/**
 *
 * @author Tom
 */
public class WatcherAgent extends Agent {

	private static final Logger logger = Logger.getLogger(WatcherAgent.class);
	private static int counter = 0;
	private TaskBarProxy taskBar;
	private String server;
	private boolean down = false;

	public WatcherAgent() {
		super();
	}

	public WatcherAgent(String server) {
		super();
		this.server = server;
	}

	/**
	 * @return the counter
	 */
	public static int getNumberOfRunningAgents() {
		return counter;
	}

	@Override
	protected void setup() {
		// Printout a hello message
		logger.log(Level.INFO, "Watcher-agent " + getAID().getName() + " starting.");

		AID receiver = LocalJade.getInstance().getTaskBarAgent(this);
		taskBar = new TaskBarProxy(this, receiver);

		//Register behaviours
		this.addBehaviour(new WatcherBehaviour(taskBar, server));
		this.addBehaviour(new CancelBehaviour());

		counter++;
	}

	@Override
	protected void takeDown() {
		// Printout a dismissal message
		logger.log(Level.INFO, "Watcher-agent " + getAID().getName() + " terminating.");

		counter--;
		down = true;
	}

	@Override
	protected void finalize() throws Throwable {
		if (!down) {
			counter--;
		}
	}
}
